#测试修改密码板块
import time

from selenium.webdriver.common.by import By
from common.Utils import CloudDriver

class Setting:
    url=""
    driver=""
    def __init__(self):
        self.url="http://vnollx.cloud/homepage"
        self.driver=CloudDriver.driver
        self.driver.get(self.url)

    def ChanPassWord(self):
        self.driver.find_element(By.CSS_SELECTOR,"#settinglink").click()
        # 切换
        iframe = self.driver.find_element(By.TAG_NAME, "iframe")
        self.driver.switch_to.frame(iframe)
        self.driver.find_element(By.CSS_SELECTOR,"body > div > div.left-section > ul > li:nth-child(2)").click()
        #输入信息
        #习惯性先清空
        self.driver.find_element(By.CSS_SELECTOR,"#old-password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#new-password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#confirm-password").clear()

        self.driver.find_element(By.CSS_SELECTOR,"#old-password").send_keys("123456")
        #新密码以及确认
        self.driver.find_element(By.CSS_SELECTOR,"#new-password").send_keys("654321")
        self.driver.find_element(By.CSS_SELECTOR,"#confirm-password").send_keys("654321")
        self.driver.find_element(By.CSS_SELECTOR,"#passwordForm > div:nth-child(4) > button").click()

        time.sleep(2)
        word=self.driver.find_element(By.CSS_SELECTOR,"#swal2-html-container").text
        print(word)
        assert word=="修改密码成功"
        self.driver.find_element(By.CSS_SELECTOR,"body > div.swal2-container.swal2-center.swal2-backdrop-show > div > div.swal2-actions > button.swal2-confirm.swal2-styled").click()

        #然后退出登录是否成功
        #注意要切换
        self.driver.switch_to.default_content()
        self.driver.find_element(By.CSS_SELECTOR,"#exit").click()
        word_exit=self.driver.find_element(By.CSS_SELECTOR,"#swal2-title").text
        print(word_exit)
        assert word_exit=="确认退出？"
        self.driver.find_element(By.CSS_SELECTOR,"body > div.swal2-container.swal2-center.swal2-backdrop-show > div > div.swal2-actions > button.swal2-confirm.swal2-styled").click()

        #判断是否退出成功
        time.sleep(2)
        assert self.driver.find_element(By.CSS_SELECTOR,"#form > h1").text=="欢迎回来"

        #重新输入新密码
        # 查找输入框，为了不出现意外先清空再输入数据
        self.driver.find_element(By.CSS_SELECTOR, "#phone").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        # 输入正确的账号和密码
        self.driver.find_element(By.CSS_SELECTOR, "#phone").send_keys("17384050350")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("654321")

        # 然后点击登录按钮
        self.driver.find_element(By.CSS_SELECTOR, "#form > button").click()
        # 判断是否登录成功，可以根据登陆之后的页面元素来判断，没找到会报错,注意页面跳转后查找元素一般需要等待一下，不然还没加载出来是找不到元素的
        # time.sleep(2)
        word = self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.sidebar > div > p").text
        assert word == "普通用户"
        print("修改后成功登录")